package com.example.websocket.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

/**
 * @author zty
 * @apiNote websocket配置类
 * @project_name the-revolt
 * @user tain
 * @create_at 2023/6/15 16:44
 * @create_vio IntelliJ IDEA
 */

//@Deprecated
@Primary
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    private final HandshakeInterceptor handshakeInterceptor;

    private final TextWebSocketHandler textWebSocketHandler;

    @Autowired
    public WebSocketConfig(HandshakeInterceptor handshakeInterceptor, TextWebSocketHandler textWebSocketHandler) {
        this.handshakeInterceptor = handshakeInterceptor;
        this.textWebSocketHandler = textWebSocketHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(textWebSocketHandler, "/ws")
                .addInterceptors(handshakeInterceptor)
                .setAllowedOrigins("*");//运行跨域访问
    }
}
